<?php

declare (strict_types=1);

namespace app\common\service\agent;

use app\common\model\agent\Channel;
use app\common\model\agent\MoneyLog;
use app\common\model\agent\Promoter;
use app\common\model\agent\Rel;
use app\common\model\HomeCallLog;
use app\common\service\BaseService;

/**
 * 代理相关服务公共类
 * Class Message
 * @package app\common\service
 */
class Common extends BaseService
{

    /**根据代理用户获取所有关联代理用户
     * @param $agent_user_id
     * @return array
     */
    public static function getRelationAgentUsers($agent_user_id):array
    {
        $low_agent_users = [];
        $condition = [
            'is_del' => 0,
            'user_id' => $agent_user_id
        ];
        $channel = Channel::where($condition)->find();
        if($channel){
            //渠道用户 = 本身 + 下级推广员
            $condition = [
                'is_del' => 0,
                'channel_id' => $channel->id
            ];
            $low_agent_users = Promoter::where($condition)->column('user_id');
        }
        return array_merge([$agent_user_id],$low_agent_users);
    }


    /** todo 根据时间获取收益
     * @param $promoter_user_id
     * @param $user_ids
     * @param $type
     */
    public static function getProfit($type,$agent_user_id): string
    {
        //包含直推和间接推用户
        $agent_user_ids = self::getRelationAgentUsers($agent_user_id);

        if($type == 'today'){
            $condition = [
                ['agent_user_id','IN', $agent_user_ids],
                ['created_at','>=', date('Y-m-d').' 00:00:00'],
                ['created_at','<',date('Y-m-d').' 23:59:59'],
            ];
        }elseif($type == 'yesterday'){
            $condition = [
                ['agent_user_id','IN', $agent_user_ids],
                ['created_at','>=', date('Y-m-d',strtotime('-1days')).' 00:00:00'],
                ['created_at','<',date('Y-m-d',strtotime('-1days')).' 23:59:59'],
            ];
        }else{
            $condition = [
                ['agent_user_id','IN', $agent_user_ids],
                ['created_at','>=', date('Y-m-d',strtotime('-30days')).' 00:00:00'],
                ['created_at','<',date('Y-m-d').' 23:59:59'],
            ];
        }
        $profit = MoneyLog::where($condition)->sum('money');

        return sprintf('%0.2f',$profit);
    }


    /**昨日活跃玩家
     * @param $agent_user_id
     */
    public static function getYesterdayActiveNum($agent_user_id): int
    {
        $condition = [
            'promoter_user_id' => $agent_user_id,
            'is_del' => 0
        ];
        $total_player_user_ids = Rel::where($condition)->column('user_id');

        $condition = [
            ['call_time','>=',strtotime(date('Y-m-d',strtotime('-1days')))],
            ['call_time','<',strtotime(date('Y-m-d',strtotime('-1days'))) + 86400]
        ];
        return HomeCallLog::whereIn('user_id',$total_player_user_ids)->where($condition)->count();
    }


    /**业绩相关
     * @param $agent_user_id
     * @param $start_time
     * @param $end_time
     * @return array
     */
    public static function getAchievement($agent_user_id,$start_time,$end_time):array
    {
        $condition1 = $condition2 = $play_condition = [];
        //时间筛选
        if(!empty($start_time) && !empty($end_time)){
            $condition1[] = ['created_at', '>=', $start_time.' 00:00:00'];
            $condition1[] = ['created_at', '<', date('Y-m-d H:i:s',strtotime($end_time) + 86400)];

            $condition2[] = ['created_at', '>=', $start_time.' 00:00:00'];
            $condition2[] = ['created_at', '<', date('Y-m-d H:i:s',strtotime($end_time) + 86400)];

            $play_condition[] = ['created_at', '>=', $start_time.' 00:00:00'];
            $play_condition[] = ['created_at', '<', date('Y-m-d H:i:s',strtotime($end_time) + 86400)];
        }
        //直推
        $condition1[] = ['agent_user_id','=',$agent_user_id];
        $direct_profit = MoneyLog::where($condition1)->sum('money');



        //累计利润
        $total_profit = $direct_profit;
        $channel = Channel::where(['is_del' => 0,'user_id' => $agent_user_id])->find();
        if($channel){
            $low_promoter_user_ids = Promoter::where(['is_del' => 0, 'channel_id' => $channel->id])->column('user_id');
            $all_agent_user_ids = array_merge($low_promoter_user_ids,[$agent_user_id]);
            $condition2[] = ['agent_user_id','IN',$all_agent_user_ids];
            $total_profit = MoneyLog::where($condition2)->sum('money');
        }

        //直属玩家数量
        $play_condition[] = ['promoter_user_id','=',$agent_user_id];
        $play_condition[] = ['is_del','=',0];
        $player_num = Rel::where($play_condition)->count();
        return [
            'direct_profit' => $direct_profit,
            'total_profit' => $total_profit,
            'player_num' => $player_num
        ];

    }


}